﻿using Assets.Scripts.UI;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Assets.Scripts.Commom
{
    public enum AudioState
    {
        Play,
        Pause,
        Stop
    }
    public class AudioSeekBar : UIBase<AudioSeekBar>, IDragHandler, IPointerUpHandler, IPointerDownHandler
    {
        public Text TotalTimeText;
        public Text AudioTimeText;

        private Slider slider;
        private AudioSource audio;
        private AudioState state;
        protected override UIType UIType
        {
            get { return UIType.Null; }
        }

        protected override void InitOnAwake()
        {
            AutoFindComponent(transform,true);
            slider = transform.GetComponent<Slider>();
        }

        private string GetTime(float time)
        {
            int minute = (int)time/60;
            int second = (int)time - minute*60;
            string s_minute = "",s_second="";
            if (minute >= 10)
            {
                s_minute = ""+minute;
            }
            else
            {
                s_minute = "0" + minute;
            }
            if (second >= 10)
            {
                s_second = "" + second;
            }
            else
            {
                s_second = "0" + second;
            }
            return s_minute + ":" + s_second;
        }

        /// <summary>
        /// 更新音频的时间
        /// </summary>
        private void UpdateAudioTime()
        {
            AudioTimeText.text = GetTime(audio.time);
            slider.value = audio.time/audio.clip.length;
        }

        public void Init(AudioSource audioSource)
        {
            slider.interactable = false;
            audio = audioSource;
            TotalTimeText.text ="/"+GetTime(audio.clip.length);
        }

        protected override void Update()
        {
            if (audio != null && audio.isPlaying)
            {
                UpdateAudioTime();
            }   
            if (audio!=null&&audio.time == audio.clip.length)//音频播放完毕
            {
                audio.Pause();
                slider.value = 0;
                SetPlayPosAndTime(0);
            }
        }

        /// <summary>
        /// 设置播放的位置和时间
        /// </summary>  
        private void SetPlayPosAndTime(float percent)
        {
            audio.time = audio.clip.length*percent;
            AudioTimeText.text = GetTime(audio.time);
        }

        public void OnDrag(PointerEventData eventData)
        {
            SetPlayPosAndTime(slider.value);
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            switch (state)
            {
                case AudioState.Play:
                    audio.Play();
                    break;
                case AudioState.Pause:
                    audio.Pause();
                    break;
            }
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            if (audio.isPlaying)
            {
                state = AudioState.Play;
            }
            else
            {
                state=AudioState.Pause;
            }
            audio.Pause();
            SetPlayPosAndTime(slider.value);
        }
    }
}